package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.platform.enhance.repository.DictionaryRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.utils.DictionaryConvert;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.domain.dictcode.dto.CreateDictCodeResponse;
import com.epoch.app.crplatformenhance.domain.dictcode.dto.QueryByCodeRequest;
import com.epoch.app.crplatformenhance.domain.dictcode.model.DictCode;
import com.epoch.app.crplatformenhance.domain.dictcode.service.DictCodeService;
import com.epoch.app.crplatformenhance.domain.dictvalue.dto.CreateDictValueResponse;
import com.epoch.app.crplatformenhance.domain.dictvalue.dto.LoadValuesByCodesRequest;
import com.epoch.app.crplatformenhance.domain.dictvalue.model.DictValue;
import com.epoch.app.crplatformenhance.domain.dictvalue.service.DictValueService;
import com.epoch.app.crplatformenhance.model.dto.*;
import com.epoch.app.crplatformenhance.sdo.DictionarySDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/10/16 21:52
 */
@Component
public class DictionaryRepositoryImpl implements DictionaryRepository {
    @Resource
    DictValueService dictValueService;
    @Resource
    DictCodeService dictCodeService;
    @Override
    public Result<List<DictionarySDO>> pageQuery(QueryDictionaryListRequest request) {
        DictCodeLoadListRequest loadListRequest = new DictCodeLoadListRequest();
        loadListRequest.setId(request.getDictId());
        loadListRequest.setCodeList(request.getDictCodes());
        loadListRequest.setName(request.getDictName());
        loadListRequest.setDesc(request.getDictDesc());
        if (CollectionUtils.isNotEmpty(request.getStatusList())){
            loadListRequest.setStatusList(request.getStatusList());
        }else {
            List<Integer> statusList = new ArrayList<>();
            statusList.add(1);
            statusList.add(0);
            loadListRequest.setStatusList(statusList);
        }

        loadListRequest.setCreatorId(request.getCreatorId());

        loadListRequest.setStart(request.getStart());
        loadListRequest.setLimit(request.getLimit());
        loadListRequest.setDir("gmt_create");
        loadListRequest.setAsc(request.getAsc());


        Result<List<DictCode>> result =  dictCodeService.loadDictCodeList(loadListRequest);
        //todo convert value 黔雷

        return null;
    }

    @Override
    public DictionarySDO querySingle(QueryDictionaryRequest request) {
        QueryByCodeRequest loadDictCodeRequest = new QueryByCodeRequest();
        loadDictCodeRequest.setId(request.getDictId());
        loadDictCodeRequest.setCode(request.getDictCode());

        DictCode dictCode = dictCodeService.queryByCode(loadDictCodeRequest);
        List<DictValue> dictValueList = new ArrayList<>();
        if (dictCode != null && dictCode.getCode() != null){
            DictValueLoadListRequest valueLoadListRequest = new DictValueLoadListRequest();
            valueLoadListRequest.setDictCode(dictCode.getCode());
            valueLoadListRequest.setLimit(500);
            valueLoadListRequest.setStart(0);

            Result<List<DictValue>> result = dictValueService.loadDictValueList(valueLoadListRequest);
            if (result!=null && CollectionUtils.isNotEmpty(result.getResult())){
                dictValueList = result.getResult();
            }
//            QueryValueListResponse valueResult =dictValueService.queryValueList(valueLoadListRequest);
//            //todo convert value 黔雷
//            if (valueResult != null && CollectionUtils.isNotEmpty(valueResult.getResult())){
//                dictValueList = valueResult.getResult();
//            }

        }

        return DictionaryConvert.convertToSdo(dictCode,dictValueList);
    }
    @Transactional
    @Override
    public String createDictionary(CreateDictionaryRequest request) {
        DictCode dictCode = new DictCode();
        dictCode.setCode(request.getCode());
        dictCode.setName(request.getName());
        dictCode.setDesc(request.getDesc());
        dictCode.setGroupCode(request.getGroupCode());
        dictCode.setStatus(1);
        dictCode.setFeatures(JSON.toJSONString(request.getFeatures()));
        dictCode.setCreatorId(request.getCreatorId());
        dictCode.setCreatorName(request.getCreatorName());
        dictCode.setModifierId(request.getModifierId());
        dictCode.setModifierName(request.getModifierName());
        dictCode.setGmtCreate(new Date());
        dictCode.setGmtModified(new Date());
        dictCode.setSyncVersion(0);
        CreateDictCodeResponse response = dictCodeService.createDictCode(dictCode);
        if (CollectionUtils.isNotEmpty(request.getDictValueList())){
            request.getDictValueList().stream().forEach(createDictValueRequest -> {
                DictValue value = new DictValue();
                value.setValueData(createDictValueRequest.getValueData());
                value.setValueName(createDictValueRequest.getValueName());
                value.setDictCode(createDictValueRequest.getDictCode());
                value.setDesc(createDictValueRequest.getDesc());
                value.setStatus(1);
                value.setPriority(createDictValueRequest.getPriority());
                value.setFeatures(JSON.toJSONString(createDictValueRequest.getFeatures()));
                value.setCreatorId(createDictValueRequest.getCreatorId());
                value.setCreatorName(createDictValueRequest.getCreatorName());
                value.setModifierId(createDictValueRequest.getModifierId());
                value.setModifierName(createDictValueRequest.getModifierName());
                value.setGmtCreate(new Date());
                value.setGmtModified(new Date());
                value.setSyncVersion(0);
                dictValueService.createDictValue(value);
            });
        }
        return response.getLastInsertId();
    }

    @Override
    public String createDictValue(CreateDictValueRequest request) {
        DictValue value = new DictValue();
        value.setValueData(request.getValueData());
        value.setValueName(request.getValueName());
        value.setDictCode(request.getDictCode());
        value.setDesc(request.getDesc());
        value.setStatus(1);
        value.setPriority(request.getPriority());
        value.setFeatures(JSON.toJSONString(request.getFeatures()));
        value.setCreatorId(request.getCreatorId());
        value.setCreatorName(request.getCreatorName());
        value.setModifierId(request.getModifierId());
        value.setModifierName(request.getModifierName());
        value.setGmtCreate(new Date());
        value.setGmtModified(new Date());
        value.setSyncVersion(0);
        CreateDictValueResponse dict =  dictValueService.createDictValue(value);
        return dict.getLastInsertId();
    }

    @Override
    public boolean updateDictCode(UpdateDictCodeRequest request) {
        return false;
    }

    @Override
    public boolean updateDictValue(UpdateDictValueRequest request) {
        return false;
    }

    @Override
    public List<DictValueSDO> queryDictValuesByCodes(QueryDictionaryListRequest request) {
        List<DictValue> dictValueList = new ArrayList<>();
        LoadValuesByCodesRequest loadValuesByCodesRequest = new LoadValuesByCodesRequest();
        loadValuesByCodesRequest.setCodeList(request.getDictCodes());
        loadValuesByCodesRequest.setValueData(request.getValueData());
        Result<List<DictValue>> result = dictValueService.loadValuesByCodes(loadValuesByCodesRequest);
        if (result!=null && CollectionUtils.isNotEmpty(result.getResult())){
            dictValueList = result.getResult();
        }
        return DictionaryConvert.convertValueSdoList(dictValueList);
    }

    @Override
    public DictValueSDO queryValueNameByCode(QueryDictionaryRequest request) {
        DictValue dictValue = dictValueService.queryValueNameByCode(request);
        if (dictValue != null){
            return DictionaryConvert.convertToValueSdo(dictValue);
        }
        return null;
    }
}
